<%@page contentType="text/html;charset=utf-8"%>
<%@ page import="java.io.File, java.io.InputStreamReader, java.io.FileInputStream" %>
<%@ page import="java.io.BufferedWriter, java.io.OutputStream, java.io.OutputStreamWriter, java.io.FileOutputStream" %>
<%@ page import="java.util.Hashtable, java.util.Vector" %>
<%@ page import="java.util.regex.Matcher, java.util.regex.Pattern" %>
<%@ page import="java.net.URL" %> 
<%@ page import="java.net.*" %> 
<%
	try{
	  //URL
	  URL url = null;
	  java.net.URLConnection URLConn = null;
	  
	  //html
	  InputStreamReader html_isr = null;
	  java.io.BufferedReader html_br = null;
	  
	  String html_line = "";
	  String html_bak = "";  //해당 url의 html
	  String html_list = "";  //html 중 list 부분
	  String html_page = "";  //html 중 page 부분
	  String html_region = "";  //html 중 지역카테고리 부분
	  
	  int iDetail = 0;
	  int iTable = 0;
	  int iEnd = 0;
	  int iPage = 0;
	  int iPageEnd = 0;
	  int iRegion = 0;
	  int iRegionEnd = 0;
	  
	  String sWhere = "";
	  String sP1 = "";
	  String sRegion = "";
	  String sStart  = "";
	  String sFull = "";
	  
	  String sbtn_num = ""; 
	  String sbtn_road = ""; 
	  sWhere = request.getParameter("where");
	  sP1 = request.getParameter("p1");
	  //sP1 = URLDecoder.decode(sP1, "UTF-8");
	  //System.out.println("222 :" + sP1);
	  sRegion = request.getParameter("region");
	  sStart = request.getParameter("start");
	  sFull = request.getParameter("full");
	  
	  String sURL = "http://nxd.search.naver.com/dsearch_r15/zipcode.naver?";
	  if ( sFull != null && sFull.length()>0 && !sFull.equals("null") && !sFull.equals("undefined")) { //full 주소로 넘어올 경우는 full 로 주소호출...
	    int iStart = sFull.indexOf("zipcodeProcess.jsp?");
	    sFull = sFull.substring(iStart+19, sFull.length());    
	    sURL += sFull;
	  } else { //full 이 아닌 경우 각 parameter 별로 정리해서 주소 호출
	    sURL += "where="+sWhere+"&p1="+sP1+"&start="+sStart;
	  }

	  if ( sURL.indexOf("http://") == 0 ) {
	    url = new java.net.URL(sURL);
	
	    URLConn = url.openConnection();
	    html_isr = new InputStreamReader(URLConn.getInputStream(), "UTF-8");
	    html_br = new java.io.BufferedReader(html_isr);
	    
	    while ( ( html_line = html_br.readLine() ) != null ) {
	      html_bak += html_line;
	    }
	    
	    if ( html_bak != null && html_bak.length() > 0 ) {
	      iDetail = html_bak.indexOf("<div class=\"detail\">");
	      iEnd = html_bak.indexOf("</div>", iDetail);
	      iPage = html_bak.indexOf("<div class=\"pagi\">");
	      iPageEnd = html_bak.indexOf("</div>", iPage);
	      iRegion = html_bak.indexOf("id=\"ds_region\"");
	      iRegionEnd = html_bak.indexOf("</ul>", iRegion);
	      if ( iDetail == -1 ) {
	        html_bak="우편번호 검색 결과가 없습니다.";
	      } else {
	        html_list = html_bak.substring(iDetail, iEnd + 6).toLowerCase();
	        iTable = html_list.indexOf("<table>");
	        iEnd = html_list.indexOf("</table>", iTable);
	        
	        html_list = html_list.substring(iTable, iEnd + 8).toLowerCase();
	        html_list = html_list.replace("<table>", "<table id=\"zipcodeTable\" cellspacing=\"0\">");
	        html_list = html_list.replace("<dl>", "");
	        html_list = html_list.replace("</dl>", "");
	        html_list = html_list.replace("<dt>", "<div>");
	        html_list = html_list.replace("</dt>", "");
	        html_list = html_list.replace("<dd>", "");
	        html_list = html_list.replace("</dd>", "</div>");
	        html_list = html_list.replace("<img src=\"http://sstatic.naver.net/keypage/lifesrch/zipcode/img/btn_num.gif\" width=\"37\" height=\"14\" alt=\"지번\">", 
	        								"<div class=\"f_l box_udos2\"><span class=\"gradation_darkgray2\">지번</span></div>");
	        html_list = html_list.replace("<img src=\"http://sstatic.naver.net/keypage/lifesrch/zipcode/img/btn_road.gif\" width=\"37\" height=\"14\" alt=\"도로명\">", 
	        								"<div class=\"f_l box_udos1\"><span class=\"gradation_red3\">도로명</span></div>");
	        
	        html_list = html_list.replace("r_addr", "addrsph");
	        
	        html_list = html_list.replace("</td> <td class=\"tc\">", "<div>");
	        html_list = html_list.replace("</strong>", "</strong></div>");
	        
	        html_list = html_list.replace("<th class=\"first_child\">주소</th> <th>우편번호</th>", "");
	        
	        html_list = html_list.replace("512px", "400px");
	        html_list = "<div id=\"divTable\">"+ html_list + "</div>";
	        
//	        html_page = html_bak.substring(iPage, iPageEnd+7).toLowerCase();
//	        html_page = html_page.replace("<div class=\"pagi\">", "<div  id=\"zipcodePage\">");
//	        html_page = html_page.replace("event)", "event); return false");
//	        html_page = html_page.replace("href=\"?", "href=\"../zipcode/zipcodeProcess.vh?");
//	        html_page = html_page.replace("clickcr", "fnSelectPage");
//	        html_page = html_page.replace("class=\"pgn\"", "");
	        //html_list = "<div id=\"divTable\">"+ html_list + html_page + "</div>";
	        
//	        if ( iRegion > -1){
//	          html_region = html_bak.substring(iRegion, iRegionEnd).toLowerCase();
//	          html_region = html_region.replace("event)", "event); return false");
//	          html_region = html_region.replace("href=\"?", "href=\"zipcodeProcess.jsp?");
//	          html_region = html_region.replace("clickcr", "fnSelectPage");
//	          html_region = html_region.replace("<li", "<div");
//	          html_region = html_region.replace("</li", "</div");
//	          html_region = html_region.replace("id=\"ds_region\"", "id=\"divRegion\" class=\"divSearch\"");
//	          html_region = "<div "+html_region+"</div>";
//	        }
	      }
	    }
//	    out.println(html_list+html_page);
	    out.println(html_list);
	  }
	} catch(Exception e){
	  e.printStackTrace();
	} finally{
	}
%>
